#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
class Solution
{
public:
    vector<vector<int>> matrixReshape(vector<vector<int>> &nums, int r, int c)
    {
        vector<int> slice;
        vector<vector<int>> ans;
        int cnt = 0;
        for (auto &vec : nums)
        {
            for (auto &i : vec)
            {
                slice.push_back(i);
                if ((++cnt % c) == 0)
                {
                    ans.push_back(slice);
                    slice.clear();
                }
            }
        }
        if (cnt != r * c)
            return nums;
        return ans;
    }
};